import { paserTaskStatus, showTimeNotSecond } from '@/libs/utils'
import TableTooltip from '@/view/drill_compete/page/tableTooltip.vue'
export const taskCol = {
  inject: ['parent'],
  components: { TableTooltip },
  props: {
    disabledFun: {
      type: Function,
      default: (data) => {
        return false
      }
    }
  },
  data() {
    return {
      paserTaskStatus,
      showTimeNotSecond,
      taskCol: [
        //              {
        //                  type: 'selection',
        //                  width: 60,
        //                  align: 'left',
        //
        //              },
        {
          title: this.$t('name'),
          key: 'name',
          minWidth: 100,
          tooltip: true
        },
        {
          title: '裁判',
          key: 'referee',
          align: 'left',
          tooltip: true,
          minWidth: 60,
          show: () => {
            return this.taskTypeTable === 'loop_report'
          }
        },
        // {
        //     title: 'TaskType',
        //     key: 'taskType',
        //      align:'center'
        // },
        // {
        //     title: this.$t('ConfigurationName'),
        //     key: 'configName',
        //     align:'center'
        // },
        // {
        //     title: 'id',
        //     key: 'id',
        //     visible:false
        // },
        {
          title: '配置',
          key: 'configName', // configName
          align: 'center',
          tooltip: true,
          width: 100,
          show: () => {
            return ['qga_read', 'qga_write', 'qga_execute_cmd', 'qga_loop_read'].includes(
              this.taskTypeTable
            )
          }
        },
        {
          title: this.$t('flagPath'),
          key: 'metadata',
          align: 'center',
          width: 100,
          tooltip: true,
          filter: (profile, data) => {
            return data.metadata.path
          },
          // render:(h,params)=>{
          //     return h('Tooltip',{
          //         'class':'tableTooltip',
          //         props:{
          //             maxWidth:400,
          //             placement:'top',
          //             triggle:'hover',
          //             transfer:true
          //         }
          //     },[
          //         h('p',{
          //             'class':'showContent',
          //             style:{
          //                 maxWidth:'100px'
          //             }
          //         },params.row.metadata.path),
          //         h('div',{
          //             'class':'tooltipContent',
          //             slot:'content'
          //         },params.row.metadata.path)
          //     ])
          // },
          show: () => {
            return ['once_configure_flag'].includes(this.taskTypeTable)
          }
        },
        {
          title: '设备名称',
          key: 'deviceData',
          align: 'center',
          tooltip: true,
          filter: (deviceData) => {
            return deviceData.map((device) => device.deviceName).join(',')
          },
          show: () => {
            return ['qga_execute_cmd', 'qga_write'].includes(this.taskTypeTable)
          }
        },
        {
          title: 'Profile',
          key: 'metadata.profile',
          align: 'center',
          filter: (profile, data) => {
            return data.metadata.profile
          },
          show: () => {
            return ['once_capture'].includes(this.taskTypeTable)
          },
          tooltip: true
        },
        {
          title: '信息',
          key: 'metadata.osInfo',
          align: 'center',
          filter: (profile, data) => {
            return data.metadata.osInfo
          },
          tooltip: true,
          show: () => {
            return this.taskTypeTable === 'once_capture'
          }
        },
        {
          title: '指令',
          key: 'metadata.cmd',
          align: 'center',
          render: (h, params) => {
            let value = params.row.metadata.cmd
            let oIP_ = []
            value.forEach((item, i) => {
              oIP_.push(`<p>${item}</p>`)
            })
            return value.length > 0
              ? h(TableTooltip, {
                  props: {
                    width: 200,
                    showContent: value.join(' '),
                    content: oIP_.join('\n')
                  }
                })
              : h('span', '无')
          },
          show: () => {
            return this.taskTypeTable === 'once_capture'
          }
        },
        //              {
        //                  title:this.$t('Desc'),
        //                  key:'description',
        //                  align:'center'
        //              },
        {
          title: '协议',
          key: 'metadata',
          align: 'center',
          render: (h, params) => {
            let metadataObj = params.row.metadata || {}
            let protoNum = metadataObj.proto || ''
            let text = ''
            if (protoNum == 1) {
              text = 'ICMP'
            } else if (protoNum == 6) {
              text = 'TCP'
            } else if (protoNum == 17) {
              text = 'UDP'
            } else if (protoNum == -1) {
              text = '暂无协议'
            }
            return h('div', text)
          },
          show: () => {
            //流量镜像
            return this.taskTypeTable === 'mirror_flow'
          }
        },
        {
          title: '协议',
          key: 'protocol',
          // align:'center',
          tooltip: true,
          minWidth: 100,
          render: (h, params) => {
            return h('div', {}, params.row.metadata.protocol)
          },
          show: () => {
            //流量镜像
            return this.taskTypeTable === 'tunnel'
          }
        },
        {
          type: 'status',
          title: this.$t('Status'),
          minWidth: 60,
          key: 'status',
          tooltip: true,
          setColor: (status, data) => {
            let color, txt
            switch (status) {
              case 'running':
                txt = 'var(--status-ing)'
                break
              case 'error':
              case 'failed':
                txt = 'var(--status-no)'
                break
              case 'completed':
                txt = 'var(--status-yes)'
                break
              case 'initialized':
                txt = 'var(--status-pre)'
                break
              case 'starting':
                txt = 'var(--status-ing)'
                break
              case 'closing':
                txt = 'var(--status-ing)'
                break
              default:
                break
            }
            return txt
          },
          filter: (status, data) => {
            let txt
            switch (status) {
              case 'running':
                txt = '运行中'
                break
              case 'error':
              case 'failed':
                txt = '失败'
                break
              case 'completed':
                txt = '完成'
                break
              case 'initialized':
                txt = '初始化'
                break
              case 'starting':
                txt = '启动中'
                break
              case 'closing':
                txt = '关闭中'
                break
              default:
                break
            }
            return txt
          }
        },
        {
          title: this.$t('Details'),
          key: '-',
          render: (h, params) => {
            let statics = params.row.overview
            let statusGroup = []
            if (JSON.stringify(statics) != '{}') {
              for (let i in statics) {
                statusGroup.push(h('span', {}, paserTaskStatus(i) + ':' + statics[i]))
              }
              return h('span', {}, statusGroup)
            } else {
              let flagUsers = params.row.flagUsers || [] //overview;
              let statics = {}
              flagUsers
                .filter((item) => item.status != 'none')
                .forEach((item) => {
                  if (!statics[item.status]) {
                    statics[item.status] = 1
                  } else {
                    statics[item.status] += 1
                  }
                })
              if (JSON.stringify(statics) != '{}') {
                for (let i in statics) {
                  statusGroup.push(h('span', {}, paserTaskStatus(i) + ':' + statics[i]))
                }
              }
              // console.log(statusGroup)
              return h('span', {}, statusGroup.length > 0 ? statusGroup : '-')
            }
          }
        },
        {
          title: this.$t('Desc'),
          key: 'description',
          minWidth: 60,
          tooltip: true,
          show: () => {
            return [
              'loop_report',
              'qga_read',
              'once_capture',
              'qga_write',
              'qga_execute_cmd',
              'loop_capture',
              'tunnel'
            ].includes(this.taskTypeTable)
          }
        },
        {
          title: this.$t('creatorName'),
          key: 'creatorName',
          minWidth: 60,
          tooltip: true
        },
        {
          title: this.$t('CTime'),
          key: 'created',
          minWidth: 60,
          tooltip: true
        },
        {
          title: this.$t('operation'),
          key: 'action',
          width: 260,
          opList: [
            {
              txt: '开始',
              show: (data) => {
                return (
                  ['initialized', 'error', 'failed', 'completed'].includes(data.status) &&
                  data.taskMode != 'normal'
                )
              },
              disabled: (data) => {
                return this.disabledAllBtn || data.taskMode == 'normal' || this.disabledFun(data)
              },
              tooltip: true,
              title: '您确定要开始此任务吗？',
              fun: (data) => {
                this.activeTask(data.id, '开始')
              }
            },
            {
              txt: '停止',
              type: 'delete',
              show: (data) => {
                return (
                  ['running', 'starting', 'stop_error'].includes(data.status) &&
                  data.taskMode != 'normal'
                )
              },
              disabled: (data) => {
                return this.disabledAllBtn || data.taskMode == 'normal' || this.disabledFun(data)
              },
              tooltip: true,
              title: '您确定要停止此任务吗？',
              fun: (data) => {
                this.activeTask(data.id, '停止')
              }
            },
            {
              txt: '修改',
              fun: (data) => {
                if (this.taskTypeTable === 'qga_write') {
                  return this.updateTask(data)
                } else {
                  return this.updateTask(data.id)
                }
              },
              disabled: (data) => {
                return this.disabledAllBtn || this.configMode === 1 || this.disabledFun(data)
              }
            },
            {
              txt: '删除',
              type: 'delete',
              tooltip: true,
              title: '您确定要删除此任务吗？',
              fun: (data) => {
                return this.deleteTask(data.id, data)
              },
              disabled: (data) => {
                return this.disabledAllBtn || this.configMode === 1 || this.disabledFun(data)
              }
            },
            {
              txt: '查看详情',
              fun: (data) => {
                this.taskDeail(data.id, data)
              }
            }
          ]
        }
      ],
      taskStatusObj: {
        initialized: {
          label: '初始化',
          value: 'initialized',
          color: 'default'
        },
        running: {
          label: '运行中',
          value: 'running',
          color: 'success'
        },
        failed: {
          label: '失败',
          value: 'failed',
          color: 'error'
        },
        completed: {
          label: '完成',
          value: 'completed',
          color: 'primary'
        },
        starting: {
          label: '启动中',
          value: 'starting',
          color: 'success'
        },
        closing: {
          label: '关闭中',
          value: 'closing',
          color: 'success'
        },
        error: {
          label: '失败',
          value: 'failed',
          color: 'error'
        }
      }
    }
  },
  methods: {
    filterTaskStatus(status) {
      return this.taskStatusObj[status].label || '未定义'
    },
    filterStatusColor(status) {
      return this.taskStatusObj[status].color || 'default'
    },
    renderTaskStatus(h, status) {
      return h(
        'Tag',
        {
          props: {
            type: 'border',
            color: this.filterStatusColor(status)
          }
        },
        this.filterTaskStatus(status)
      )
    }
  }
}
export const validateName = (rule, value, callback) => {
  var myReg = /[~!！@#$%^&*()/\|,<>?"'“‘();:+=\[\]{}]/
  value = value.replace(/\s*/g, '')
  if (!value) {
    callback('请填写名字')
  } else {
    if (myReg.test(value)) {
      callback('名字不能包含特殊字符')
    } else {
      callback()
    }
  }
}
